package com.example.kafka;

import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.Properties;

/**
 * @author Jonathan
 * 2021/5/5 21:49
 */
@Slf4j
public class ProducerFastStart {
    public static final String brokerList = "192.168.1.103:9092";
    public static final String topic = "topic-demo";

    public static void main(String[] args) {
        Properties properties = new Properties();
        properties.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        properties.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        properties.put("bootstrap.servers", brokerList);
        KafkaProducer<String, String> producer = new KafkaProducer<>(properties);
        ProducerRecord<String, String> record = new ProducerRecord<>(topic, "hello, Kafka!");
        try {
            log.info("hello");
            producer.send(record);
            log.info("ret:{}", producer.send(record).get());
        } catch (Exception e) {
            System.out.println("error.");
            log.info("Error:", e.fillInStackTrace());
        }
        System.out.println("he");
        producer.close();
    }
}
